package com.agm.android.fragments;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.agm.android.binder.BinderGiocatore;
import com.agm.android.constants.Constants;
import com.agm.dto.Giocatore;
import com.example.android.navigationdrawerexample.App;
import com.example.android.navigationdrawerexample.R;
import com.fasterxml.jackson.databind.ObjectMapper;

public class RosaFragment extends Fragment {
	public static final String ARG_PLANET_NUMBER = "planet_number";

	public RosaFragment() {
		// Empty constructor required for fragment subclasses
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_rosa, container,false);
		if(App.rosa != null && App.lega != null){
			ListView listView = (ListView) rootView.findViewById(R.id.listViewRosa);
			List<String> arrayGiocatori = new ArrayList<String>();
			for(Giocatore giocatore : App.rosa){
				arrayGiocatori.add(giocatore.getCognome() +" - "+giocatore.getRuolo() +" - "+giocatore.getQuotazione() + " - "+giocatore.getStato());
			}
	
			List<HashMap<String, String>> contentDataCollection = new ArrayList<HashMap<String, String>>();
	        HashMap<String, String> map = null;
	        
	        for(Giocatore giocatore : App.rosa){
	        	 map = new HashMap<String, String>();
	             map.put(BinderGiocatore.KEY_COGNOME, giocatore.getCognome());
	             map.put(BinderGiocatore.KEY_IMAGE_STATO,giocatore.getStato().toLowerCase());
	             map.put(BinderGiocatore.KEY_RUOLO, giocatore.getRuolo());
	           
	             contentDataCollection.add(map);
			}
	        
	        BinderGiocatore bindingData = new BinderGiocatore(inflater, contentDataCollection);
	        listView.setAdapter(bindingData);
	
	//        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.rowrosa, R.id.textViewList, arrayGiocatori);
	//		listView.setAdapter(arrayAdapter);
			if(App.lega != null){
				TextView nome = (TextView) rootView.findViewById(R.id.nomesquadraValue);
				TextView supporters = (TextView) rootView.findViewById(R.id.supportersValue);
				TextView arena = (TextView) rootView.findViewById(R.id.arenaValue);
				TextView crediti = (TextView) rootView.findViewById(R.id.creditiValue);
				TextView posizione = (TextView) rootView.findViewById(R.id.posizioneValue);
				
				nome.setText(App.lega.getNomesquadra());
				arena.setText(App.lega.getArena());
				supporters.setText(App.lega.getSupporters());
				crediti.setText("" + App.lega.getCrediti());
				posizione.setText("" + App.lega.getPosizione());
			}
		}
		return rootView;
	}
	
	/**
	 * Represents an asynchronous login/registration task used to authenticate
	 * the user.
	 */
	public class ClassificaTask extends AsyncTask<Void, Void, Boolean> {
		@Override
		protected Boolean doInBackground(Void... params) {
			JSONObject holder = new JSONObject();
			DefaultHttpClient httpclient = new DefaultHttpClient();
			ObjectMapper mapper = new ObjectMapper();
			HttpPost httpost = new HttpPost(Constants.URL_REST + "getRosa");
			try {
				
				holder.put(new String("username"), App.utente.getNome());
				holder.put(new String("password"), App.utente.getPassword());
				holder.put(new String("idlega"), App.utente.getIdlegaprivata());
				StringEntity se = new StringEntity(holder.toString());

				httpost.setEntity(se);
				
				httpost.setHeader("Accept", "application/json");
				httpost.setHeader("Content-type", "application/json");
				HttpResponse response = httpclient.execute(httpost);
				HttpEntity httpEntity = response.getEntity();
				InputStream is = httpEntity.getContent();
				App.rosa = mapper.readValue(is, Giocatore[].class);
			} catch (JSONException e) {
				e.printStackTrace();
				return false;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return false;
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				return false;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			} 

			return true;
		}
		
		@Override
		protected void onPostExecute(final Boolean success) {
		}

		@Override
		protected void onCancelled() {
		
		}
	}
}
